package com.xbongbong.pro.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.rabbitmq.binders.ScoreConsumerBinder;
import com.xbongbong.pro.rabbitmq.constant.ScoreBinderConstant;
import com.xbongbong.pro.rabbitmq.feign.ScoreFeignClient;
import com.xbongbong.pro.score.pojo.dto.ScoreRabbitMqDTO;
import com.xbongbong.pro.score.pojo.vo.ScoreMessageRabbitMqVO;
import com.xbongbong.pro.sms.pojo.vo.SmsRabbitMqVO;
import feign.RetryableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.scheduling.annotation.Async;

import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;

/**
 * 评分消息消费者
 * @author chunyang.qin
 * @date 2019/3/19 15:32
 * @since v1.0
 * @version v1.0
 */
@EnableBinding(ScoreConsumerBinder.class)
public class ScoreListener {
    private static Logger logger = LoggerFactory.getLogger(ScoreListener.class);


    @Resource
    private ScoreFeignClient scoreFeignClient;

    /**
     *
     * @param scoreRabbitMqDTO 评分实体类
     */
    @StreamListener(value = ScoreBinderConstant.SCORE_INPUT)
    @Async(value = "scoreScriptThreadPool")
    public void scoreReceive(ScoreRabbitMqDTO scoreRabbitMqDTO) {
        try {
            long startTime = System.currentTimeMillis();
            XbbResponse<SmsRabbitMqVO> response = null;
            try {
                logger.info("ScoreListener 收到消息  Received:  message=" + JSON.toJSONString(scoreRabbitMqDTO));
                XbbResponse<ScoreMessageRabbitMqVO> scoreMessageRabbitMqVOXbbResponse = scoreFeignClient.scoreUpdate(scoreRabbitMqDTO);
                logger.info("ScoreListener response=" + JSON.toJSONString(scoreMessageRabbitMqVOXbbResponse));
                long endTime = System.currentTimeMillis();
            } catch (RetryableException e) {
                logger.error("ScoreListener.scoreReceive RetryableException case by Read timed out");
            } catch (Exception e) {
                logger.error("ScoreListener.scoreReceive Exception, e-{}-" , e.getMessage());
            }
        }catch (Exception e){
            logger.error("{}",e.getMessage());
        }

    }
}
